home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / cpptask.exe / TSKKBD.ASM < prev    next >
Assembly Source File  |  1991-07-26  |  5KB  |  310 lines

  1. ;
  2. ;    CTask - Keyboard handler module.
  3. ;
  4. ;    Public Domain Software written by
  5. ;        Thomas Wagner
  6. ;        Patschkauer Weg 31
  7. ;        D-1000 Berlin 33
  8. ;        West Germany
  9. ;
  10. ;    This module traps the keyboard interrupts to allow task switching
  11. ;    on waiting for a character.
  12. ;    All characters are put into a (word)pipe, and can either be
  13. ;    read directly from the pipe, or through the normal INT 16 channels.
  14. ;
  15.     name    tskkbd
  16.     .model    large
  17. ;
  18.     public    _tsk_install_kbd
  19.     public    _tsk_remove_kbd
  20. ;
  21.     public    _t_read_key
  22.     public    _t_wait_key
  23.     public    _t_keyhit
  24. ;
  25.     extrn    _asm_read_wpipe: far
  26.     extrn    _asm_check_wpipe: far
  27.     extrn    _asm_c_write_wpipe: far
  28. ;
  29.     include    tsk.mac
  30. ;
  31. intseg    segment at 0
  32.         org    09h*4
  33. hwdoff        dw    ?    ; keyboard hardware interrupt
  34. hwdseg        dw    ?
  35.         org    16h*4
  36. kbdoff        dw    ?    ; keyboard I/O interrupt
  37. kbdseg        dw    ?
  38. ;
  39. intseg    ends
  40. ;
  41. ;----------------------------------------------------------------------------
  42. ;
  43. ;    Variables
  44. ;
  45.     extrn _key_avail: wpipe
  46. ;
  47.     .code
  48. ;
  49. kbd_flag    db    0
  50. ;
  51. ;    Original Interrupt-Entries
  52. ;
  53. savhwd        label    dword        ; original hardware int entry
  54. savhwdoff    dw    ?
  55. savhwdseg    dw    ?
  56. ;
  57. savkbd        label    dword        ; original keyboard I/O entry
  58. savkbdoff    dw    ?
  59. savkbdseg    dw    ?
  60. ;
  61. ;
  62. ;---------------------------------------------------------------------------
  63. ;
  64.     .code
  65. ;
  66. ;
  67. ;    void tsk_install_kbd (void)
  68. ;
  69. ;        Install keyboard handler
  70. ;
  71. _tsk_install_kbd    proc    far
  72. ;
  73. ;
  74. ;    Save old interrupt vectors
  75. ;
  76.         push    es
  77.     xor    ax,ax
  78.     mov    es,ax
  79. ;
  80.         assume  es:intseg
  81. ;
  82.     mov    ax,kbdoff
  83.     mov    savkbdoff,ax
  84.     mov    ax,kbdseg
  85.     mov    savkbdseg,ax
  86. ;
  87.     mov    ax,hwdoff
  88.     mov    savhwdoff,ax
  89.     mov    ax,hwdseg
  90.     mov    savhwdseg,ax
  91. ;
  92. ;    Enter new Interrupt-Entries
  93. ;
  94.     cli
  95.     mov    kbdoff,offset kbdentry
  96.     mov    kbdseg,cs
  97.     mov    hwdoff,offset hwdentry
  98.     mov    hwdseg,cs
  99.     sti
  100.         pop     es
  101. ;
  102.     ret
  103. ;
  104.     assume    es:nothing
  105. ;
  106. _tsk_install_kbd    endp
  107. ;
  108. ;
  109. ;    void tsk_remove_kbd (void)
  110. ;
  111. ;        Un-install keyboard handler
  112. ;
  113. _tsk_remove_kbd    proc    far
  114. ;
  115.         push    es
  116.     xor    ax,ax
  117.     mov    es,ax
  118. ;
  119.         assume  es:intseg
  120. ;
  121. ;    Restore interrupt entries
  122. ;
  123.     cli
  124. ;
  125.     mov    ax,savkbdoff
  126.     mov    kbdoff,ax
  127.     mov    ax,savkbdseg
  128.     mov    kbdseg,ax
  129. ;
  130.     mov    ax,savhwdoff
  131.     mov    hwdoff,ax
  132.     mov    ax,savhwdseg
  133.     mov    hwdseg,ax
  134. ;
  135.     sti
  136. ;
  137.         pop     es
  138.     ret
  139. ;
  140.     assume    es:nothing
  141. ;
  142. _tsk_remove_kbd    endp
  143. ;
  144. ;
  145. ;---------------------------------------------------------------------------
  146. ;
  147. ;    int t_read_key (void)
  148. ;
  149. ;    Waits for key from keyboard. Returns char in lower byte,
  150. ;    Scan-Code in upper byte.
  151. ;
  152. _t_read_key    proc    far
  153. ;
  154.     xor    ax,ax        ; no timeout
  155.     push    ax
  156.     push    ax
  157.     mov    ax,offset _key_avail
  158.     push    ds
  159.     push    ax
  160.     call    _asm_read_wpipe
  161.     add    sp,8
  162.     ret
  163. ;    
  164. _t_read_key    endp
  165. ;
  166. ;
  167. ;    int t_wait_key (dword timeout)
  168. ;
  169. ;    Waits for key from keyboard. Returns char in lower byte,
  170. ;    Scan-Code in upper byte.
  171. ;
  172. _t_wait_key    proc    far
  173. ;
  174.     push    bp
  175.     mov    bp,sp
  176.     push    8[bp]        ; timeout
  177.     push    6[bp]
  178.     mov    ax,offset _key_avail
  179.     push    ds
  180.     push    ax
  181.     call    _asm_read_wpipe
  182.     add    sp,8
  183.     pop    bp
  184.     ret
  185. ;    
  186. _t_wait_key    endp
  187. ;
  188. ;
  189. ;    int t_keyhit (void)
  190. ;
  191. ;    Checks if char is available. Returns -1 if not, else the
  192. ;    character value. The character remains in the buffer.
  193. ;
  194. _t_keyhit    proc    far
  195. ;
  196.     mov    ax,offset _key_avail
  197.     push    ds
  198.     push    ax
  199.     call    _asm_check_wpipe
  200.     add    sp,4
  201.     ret
  202. ;
  203. _t_keyhit    endp
  204. ;
  205. ;---------------------------------------------------------------------------
  206. ;
  207. ;    INT 9 - Keyboard hardware interrupt
  208. ;
  209. hwdentry    proc    far
  210. ;
  211.     pushf
  212.     call    cs:savhwd    ; process key
  213.     push    ds
  214.     push    es
  215.     push    ax
  216.     push    bx
  217.     push    cx
  218.     push    dx
  219.     mov    ax,seg dgroup
  220.     mov    ds,ax
  221.     mov    es,ax
  222. ;
  223.     mov    ah,1
  224.     pushf
  225.     call    cs:savkbd    ; check if char available
  226.     jz    hwd_ret        ; return if not
  227. ;
  228.     xor    ah,ah    
  229.     pushf
  230.     call    cs:savkbd    ; get the key
  231. ;
  232.     push    ax        ; push key
  233.     mov    ax,offset _key_avail
  234.     push    ds
  235.     push    ax
  236.     call    _asm_c_write_wpipe
  237.     add    sp,6
  238. ;
  239. hwd_ret:
  240.     pop    dx
  241.     pop    cx
  242.     pop    bx
  243.     pop    ax
  244.     pop    es
  245.     pop    ds
  246.     iret
  247. ;
  248. hwdentry    endp
  249. ;
  250. ;---------------------------------------------------------------------------
  251. ;
  252. ;    INT 16 - Keyboard I/O
  253. ;
  254. kbdentry    proc    far
  255. ;
  256.     cmp    ah,2
  257.     jb    kbd_funcs
  258.     jmp    cs:savkbd    ; pass on functions >= 2
  259. ;    
  260. kbd_funcs:
  261.     sti
  262.     push    ds
  263.     push    es
  264.     push    dx
  265.     push    cx
  266.     push    bx
  267.     mov    bx,seg dgroup
  268.     mov    ds,bx
  269.     mov    es,bx
  270. ;
  271.     cmp    ah,1
  272.     jb    kbd_read
  273. ;
  274.     mov    bx,offset _key_avail
  275.     push    ds
  276.     push    bx
  277.     call    _asm_check_wpipe
  278.     add    sp,4
  279.     cmp    ax,0ffffh
  280.     jne    kbd_gotone
  281.     xor    ax,ax
  282. kbd_gotone:
  283.     pop    bx
  284.     pop    cx
  285.     pop    dx
  286.     pop    es
  287.     pop    ds
  288.     ret    2
  289. ;
  290. kbd_read:
  291.     xor    ax,ax
  292.     push    ax
  293.     push    ax
  294.     mov    bx,offset _key_avail
  295.     push    ds
  296.     push    bx
  297.     call    _asm_read_wpipe
  298.     add    sp,8
  299.     pop    bx
  300.     pop    cx
  301.     pop    dx
  302.     pop    es
  303.     pop    ds
  304.     iret
  305. ;
  306. kbdentry    endp
  307. ;
  308.     end
  309.  
  310.